package com.xiaotu.spider.baidu.pageprocessor;

import com.xiaotu.common.model.StarAwardRecordModel;
import com.xiaotu.common.util.*;
import com.xiaotu.server.service.guduo.SupplementGuDuoRecordService;
import com.xiaotu.spider.AUrlPageProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名 StarAwardBaikePageProcessor
 * @日期 2021年2月7日
 * @作者 王艳龙
 * @功能 百度百科艺人获奖记录
 */
@Component("StarAwardBaikePageProcessor")
public class StarAwardBaikePageProcessor extends AUrlPageProcessor
{
    private String start_url = "https://www.baidu.com/";
	
    @Autowired
	private SupplementGuDuoRecordService starService;
    
    @Override
	protected String[] getStartRequestUrls() {
    	return new String[]{start_url};
	}
	
    @Override
   	public void process(Page page) {
    	if (Constants.PageDataType.STAR_RECORD.equals(this.getPageDataType(page)))
            this.tvRecord(page);
        else
            this.pageProcess(page);
    }
    
    private void pageProcess(Page page) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("rows",5000);
		param.put("page",33);
    	List<Map<String,Object>> starList = starService.getAllStarInfoList(param);
    	for (Map<String, Object> starInfoMap : starList) {
			String linkUrl = "https://baike.baidu.com/item/"+ KeywordEncode.encode(starInfoMap.get("name")+"")+"?" +
					"starid="+starInfoMap.get("starid");
			PageUtils.addTargetRequest(page, linkUrl, page.getRequest().getExtras());
    	}
    }
    
	private void tvRecord(Page page) {
		String linkUrl = KeywordEncode.decode(page.getUrl().toString());
		//String resOccupation=null;
		//String resWorks = null;
		String starName = null;
		String starId = null;
		starName = linkUrl.substring(linkUrl.indexOf("item/")+5, linkUrl.indexOf("?starid"));
		starId = linkUrl.substring(linkUrl.indexOf("starid=")+7, linkUrl.length());
		String allAward = page.getHtml().xpath("//table[contains(@class,'horizontal-module j-module')]").get();
		int status =0;
		//判断是否有获奖记录   获奖状态  0:无获奖，1：有获奖
		if(StringUtil.isEmpty(allAward)){
			status=0;
		}else{
			status=1;
		}
		page.putField(StarAwardRecordModel.class.getName(),new StarAwardRecordModel(starId,starName,status));
		/*resOccupation = linkUrl.substring(linkUrl.indexOf("occupation=")+11, linkUrl.indexOf("&works"));
		resWorks = linkUrl.substring(linkUrl.indexOf("&works=")+7, linkUrl.length());
		List<String> nameList = page.getHtml().xpath("//dt[@class='basicInfo-item name']").all();
		List<String> valueList = page.getHtml().xpath("//dd[@class='basicInfo-item value']").all();
		String name = null, occupation=null,works=null;
		boolean occflag = false;
		boolean worksflag = false;
		if (nameList != null && !nameList.isEmpty() && nameList.size() == valueList.size()){
			for (int j = 0; j < nameList.size(); j++) {
				String nameType =nameList.get(j);
				nameType =nameType.trim().replaceAll(" ","");
				*//*if(nameType.contains("中文名")){
					name = valueList.get(j);
				}*//*
				if(nameType.contains("职") && nameType.contains("业")){
					occupation = valueList.get(j);
				}
				if(nameType.contains("代表作品")){
					works = valueList.get(j);
				}
			}
			//String resOccupation = starInfoMap.get("occupation")+"";
			if(StringUtil.isNotEmpty(resOccupation)){
				String[] occupationArr = resOccupation.split(",");
				for (String occupationStr : occupationArr) {
					if(StringUtil.isNotEmpty(occupation)){
						if(occupation.contains(occupationStr)){
							occflag = true;
							break;
						}
					}
				}
			}
			if(StringUtil.isNotEmpty(resWorks)){
				String[] resWorksArr = resWorks.split(",");
				for (String worksStr : resWorksArr) {
					if(StringUtil.isNotEmpty(works)){
						if(works.contains(worksStr)){
							worksflag = true;
							break;
						}
					}
				}
			}
			if(StringUtil.isNotEmpty(resWorks)){
				if(occflag && worksflag){
					String allAward = page.getHtml().xpath("//table[contains(@class,'horizontal-module j-module')]").get();
					int status =0;
					//判断是否有获奖记录   获奖状态  0:无获奖，1：有获奖
					if(StringUtil.isEmpty(allAward)){
						status=0;
					}else{
						status=1;
					}
					page.putField(StarAwardRecordModel.class.getName(),new StarAwardRecordModel(starId,starName,status));
				}
			}else{
				if(occflag){
					String allAward = page.getHtml().xpath("//table[contains(@class,'horizontal-module j-module')]").get();
					int status =0;
					//判断是否有获奖记录   获奖状态  0:无获奖，1：有获奖
					if(StringUtil.isEmpty(allAward)){
						status=0;
					}else{
						status=1;
					}
					page.putField(StarAwardRecordModel.class.getName(),new StarAwardRecordModel(starId,starName,status));
				}else{
					String allAward = page.getHtml().xpath("//table[contains(@class,'horizontal-module j-module')]").get();
					int status =0;
					//判断是否有获奖记录   获奖状态  0:无获奖，1：有获奖
					if(StringUtil.isEmpty(allAward)){
						status=0;
					}else{
						status=1;
					}
					page.putField(StarAwardRecordModel.class.getName(),new StarAwardRecordModel(starId,starName,status));
				}
			}

		}*/
    }
    
	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.STAR_AWARD_BAIKE;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
        if (url.contains("item"))
        	return Constants.PageDataType.STAR_RECORD;
		return Constants.PageDataType.SEARCH_LIST;
	}
}
